package com.googlecode.aleverr.yafotkiclient.ui.adapters;

import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import com.googlecode.aleverr.yafotkiclient.R;
import com.googlecode.aleverr.yafotkiclient.api.Photo;

public class PhotoGridAdapter extends AbstractPhotoAdapter<Photo> {

    private static final int PROGRESS_BAR_INDEX = 0;
    private static final int IMAGE_VIEW_INDEX = 1;

    @Override
    protected void processElement(View view, Photo photo) {
        ViewSwitcher viewSwitcher = (ViewSwitcher) view;
        viewSwitcher.setDisplayedChild(PROGRESS_BAR_INDEX);
        PHOTO_LOADER.queuePhotoLoad(viewSwitcher, photo, this);
    }

    @Override
    protected View loadView(ViewGroup viewGroup) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        return inflater.inflate(R.layout.photo_grid_item, viewGroup, false);
    }

    @Override
    public void assignBitmapToView(Bitmap bitmap, View view) {
        if (bitmap != null) {
            ImageView imageView = (ImageView) view.findViewById(R.id.photo);
            imageView.setImageBitmap(bitmap);
        }
        if (view instanceof ViewSwitcher) {
            ((ViewSwitcher) view).setDisplayedChild(IMAGE_VIEW_INDEX);
        }
    }
}
